#include "Enemy.h"

Enemy::Enemy()
	: mActive(false), mMovementDelay(0.0f) //why is this better?
{
}

Enemy::~Enemy()
{
}

void Enemy::Load()
{
	mSprite.Load("pikachu.png");
}

void Enemy::Unload()

{
	mSprite.Unload();
}

void Enemy::Update(float deltaTime)
{
	//only update active Enemys
	if(mActive)
	{
		mMovementDelay -= deltaTime;//sub the data
		if(mMovementDelay <= 0.0f)//when reach 0
		{
			mVelocity.x *= (-1.0f);//flip direction
			mMovementDelay = RandomFloat(0.5f, 1.5f);
		}
		
			mPosition += mVelocity * deltaTime;
			const int kWinWidth = IniFile_GetInt("WinWidth", 800) + (mSprite.GetWidth() / 2);
			const int kWinHeight = IniFile_GetInt("WinHeight", 600) + (mSprite.GetHeight() /2);
		if(mPosition.x < 0 || mPosition.x > kWinWidth ||
			mPosition.y < 0|| mPosition.y > kWinHeight)
		{
			Kill();
		}
	}

}

void Enemy::Render()
{
	//only render active Enemys
	if(mActive)
	{
		const int width = mSprite.GetWidth();
		const int height = mSprite.GetHeight();

		SVector2 offset(width * 0.5f, height * 0.5f);
		SVector2 renderPosition = mPosition - offset;

		mSprite.SetPosition(renderPosition);
		mSprite.Render();
	}
}

void Enemy::Spawn(const SVector2& pos)
{
	mMovementDelay = RandomFloat(0.3f, 2.0f);	

	mPosition = pos;
		mVelocity = SVector2(RandomFloat(0.0f, 200.0f), 200);
		mActive = true;
}

void Enemy::Kill()
{
	//set false to stop update and render functions
	mActive = false;
}